﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【角色与权限】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto角色与权限
    /// </summary>
    public abstract class AutoRolePermissionMapping : BaseMapping<RolePermission, RolePermissionModel, RolePermissionFilterModel>
    {
        public AutoRolePermissionMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override RolePermissionModel Entity2Model(RolePermission entity)
        {
            RolePermissionModel model = new RolePermissionModel();

            model.RolePermissionID = entity.RolePermissionID;
            model.GroupInfoID = entity.GroupInfoID;
            model.RoleID = entity.RoleID;
            model.PermissionID = entity.PermissionID;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override RolePermission Model2Entity(RolePermissionModel model)
        {
            RolePermission entity = new RolePermission();

            entity.RolePermissionID = model.RolePermissionID;
            entity.GroupInfoID = model.GroupInfoID;
            entity.RoleID = model.RoleID;
            entity.PermissionID = model.PermissionID;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<RolePermission> FilterModel2Where(RolePermissionFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._RolePermissionID.HasValue)
            {
                where = where.And(x => x.RolePermissionID == filterModel._RolePermissionID.Value);
            }
            if (filterModel._RolePermissionIDs.IsNotEmpty() && filterModel._RolePermissionIDs.Count > 0)
            {
                where = where.And(x => filterModel._RolePermissionIDs.Contains(x.RolePermissionID));
            }
            if (filterModel._NotRolePermissionIDs.IsNotEmpty() && filterModel._NotRolePermissionIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotRolePermissionIDs.Contains(x.RolePermissionID));
            }

            //集团
            if (filterModel._GroupInfoID.HasValue)
            {
                where = where.And(x => x.GroupInfoID == filterModel._GroupInfoID.Value);
            }
            if (filterModel._GroupInfoIDs.IsNotEmpty() && filterModel._GroupInfoIDs.Count > 0)
            {
                where = where.And(x => filterModel._GroupInfoIDs.Contains(x.GroupInfoID));
            }
            if (filterModel._NotGroupInfoIDs.IsNotEmpty() && filterModel._NotGroupInfoIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotGroupInfoIDs.Contains(x.GroupInfoID));
            }

            //用户角色
            if (filterModel._RoleID.HasValue)
            {
                where = where.And(x => x.RoleID == filterModel._RoleID.Value);
            }
            if (filterModel._RoleIDs.IsNotEmpty() && filterModel._RoleIDs.Count > 0)
            {
                where = where.And(x => filterModel._RoleIDs.Contains(x.RoleID));
            }
            if (filterModel._NotRoleIDs.IsNotEmpty() && filterModel._NotRoleIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotRoleIDs.Contains(x.RoleID));
            }

            //权限
            if (filterModel._PermissionID.HasValue)
            {
                where = where.And(x => x.PermissionID == filterModel._PermissionID.Value);
            }
            if (filterModel._PermissionIDs.IsNotEmpty() && filterModel._PermissionIDs.Count > 0)
            {
                where = where.And(x => filterModel._PermissionIDs.Contains(x.PermissionID));
            }
            if (filterModel._NotPermissionIDs.IsNotEmpty() && filterModel._NotPermissionIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotPermissionIDs.Contains(x.PermissionID));
            }

            return where;
        }
        #endregion
    }
}

